bst

Unique Binary Search Trees-计算表示相同序列的不同BST个数

无人久伴 提交于 2020-01-27 04:35:13
题目描述: 给定整数n,计算存储序列为1...n的结构唯一的BST的个数 题目来源 : http://oj.leetcode.com/problems/unique-binary-search-trees/ 题目分析: 对于一个表示序列为1...n的BST,根元素可以是1到n中的任何一个数,当根元素为 i 时,左子树为表示1...i - 1的BST,右子树为表示i + 1...n的BST,所以,原问题可以通过子问题的解得到 定义状态f(i,j),状态f(i,j)为表示序列i...j的结构唯一的BST的个数,通过枚举根的值可以得到: f(i,j) = sum(f(i,k - 1) * f(k + 1, j))(i <= k <= j) 时间复杂度: O(n^3) 示例代码: int dp[100][100]; int numTrees(int n) { memset(dp, 0, sizeof(dp)); for(int i = 1; i <= n; ++i) dp[i][i - 1] = dp[i + 1][i] = 1; for(int len = 1; len <= n; ++len) { for(int i = 1; i <= n - len + 1; ++i) { for(int k = i; k <= i + len - 1; ++k) { dp[i][i + len -

530. Minimum Absolute Difference in BST

半世苍凉 提交于 2020-01-26 15:41:43
530. 二叉搜索树的最小绝对差 给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值。 示例 : 输入: 1 \ 3 / 2 输出: 1 解释: 最小绝对差为1,其中 2 和 1 的差的绝对值为 1(或者 2 和 3)。 注意: 树中至少有2个节点。 解法一 //时间复杂度O(n^2), 空间复杂度O(n) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void buildArray(TreeNode* root) { if(root == nullptr) return; rec.push_back(root->val); buildArray(root->left); buildArray(root->right); }; int getMinimumDifference(TreeNode* root) { buildArray(root); int minAbs = INT_MAX; for(int i = 1

653. Two Sum IV - Input is a BST

て烟熏妆下的殇ゞ 提交于 2020-01-24 21:51:13
653. 两数之和 IV - 输入 BST 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。 案例 1: 输入: 5 / \ 3 6 / \ \ 2 4 7 Target = 9 输出: True 案例 2: 输入: 5 / \ 3 6 / \ \ 2 4 7 Target = 28 输出: False 解法一 //时间复杂度O(n), 空间复杂度O(n) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool findTarget(TreeNode* root, int k) { if(!root) return false; if(findTarget(root->left, k)) return true; if(us.count(root->val)) return true; us.insert(k - root->val); if(findTarget(root-

调整搜索二叉树的两个错误的节点

依然范特西╮ 提交于 2020-01-23 01:47:18
题目 一棵二叉查找树,其中两个节点的值调换了位置,找出这两个错误节点。 TreeNode class TreeNode ( ) : def __init__ ( self , val , left = None , right = None ) : self . val = val self . left = left self . right = right BinarySearchTree class BinarySearchTree ( ) : def __init__ ( self ) : self . root = None def insert ( self , val ) : def recursive ( node , val ) : if node is None : return TreeNode ( val ) if val < node . val : node . left = recursive ( node . left , val ) elif val > node . val : node . right = recursive ( node . right , val ) return node self . root = recursive ( self . root , val ) def inorder_traversal ( self )

PAT Advanced 1043 Is It a Binary Search Tree (25分)

人走茶凉 提交于 2020-01-23 00:47:37
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than or equal to the node's key. Both the left and right subtrees must also be binary search trees. If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST. Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image

783. Minimum Distance Between BST Nodes

你离开我真会死。 提交于 2020-01-22 08:28:36
783. 二叉搜索树结点最小距离 给定一个二叉搜索树的根结点 root , 返回树中任意两节点的差的最小值。 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(TreeNode object),而不是数组。 给定的树 [4,2,6,1,3,null,null] 可表示为下图: 4 / \ 2 6 / \ 1 3 最小的差值是 1, 它是节点1和节点2的差值, 也是节点3和节点2的差值。 注意: 二叉树的大小范围在 2 到 100 。 二叉树总是有效的,每个节点的值都是整数,且不重复。 解法一 //时间复杂度O(n), 空间复杂度O(n) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void minDiffInBST(TreeNode* root, TreeNode*& last, int& minDiff) { if(!root) return;

1115 Counting Nodes in a BST

╄→尐↘猪︶ㄣ 提交于 2020-01-21 20:49:07
自己的方式是先建树,然后进行层序遍历,记录每层node数量,输出最后两层的个数。但比较麻烦。 网上看到两个相对优秀简洁的算法思路,一是在建树的同时,如果插入节点是左右孩子都不存在,相当于if(root == NULL)的else,那么插入也即增加一个深度,然后在if 语句中用数组记录该深度下插入节点的格式。 二是在建树完成后,用深度优先算法进行快速遍历,带出每层个数 # include <iostream> # include <queue> using namespace std ; const int maxn = 1001 ; int rec [ maxn ] = { 0 } ; struct node { int data , layer ; node * lchild , * rchild ; } ; void insert ( node * & root , int x ) { if ( root == NULL ) { root = new node ; root - > data = x ; root - > lchild = root - > rchild = NULL ; return ; } if ( x <= root - > data ) insert ( root - > lchild , x ) ; else insert ( root - >

BST

≯℡__Kan透↙ 提交于 2020-01-20 03:47:28
纸不短,情不长 /** * @Classname BST * @Description * @Date 2020/1/19 10:27 * @Author SonnSei */ public class BST < E extends Comparable < E > > { private Node root ; private int size ; public BST ( ) { root = null ; size = 0 ; } public int getSize ( ) { return size ; } public boolean isEmpty ( ) { return size == 0 ; } public void add ( E e ) { root = add ( root , e ) ; } private Node add ( Node node , E e ) { if ( node == null ) { size ++ ; return new Node ( e ) ; } if ( e . compareTo ( node . value ) < 0 ) node . left = add ( node . left , e ) ; else if ( e . compareTo ( node . value ) > 0 ) node .

Hackerrank Day 23: BST Level-Order Traversal 逐层遍历

*爱你&永不变心* 提交于 2020-01-11 16:10:43
CODE import sys class Node: def __init__(self,data): self.right=self.left=None self.data = data class Solution: def insert(self,root,data): if root==None: return Node(data) else: if data<=root.data: cur=self.insert(root.left,data) root.left=cur else: cur=self.insert(root.right,data) root.right=cur return root def levelOrder(self,root): #Write your code here nodes=[]#将节点存在list中 results='' if root.data is not None: nodes.append(root) while nodes: tmp=nodes[0] results=results+str(tmp.data)+' ' #print(str(tmp.data)+' ',end='') if tmp.left is not None: nodes.append(tmp.left) if tmp.right is not

图解:二叉搜索树算法(BST)

僤鯓⒐⒋嵵緔 提交于 2020-01-07 12:31:09
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “岁月极美,在于它必然的流逝” “春花 秋月 夏日 冬雪” — 三毛 一、树 & 二叉树 树 是由节点和边构成,储存元素的集合。节点分根节点、父节点和子节点的概念。 如图:树深=4; 5是根节点;同样8与3的关系是父子节点关系。 二叉树binary tree ,则加了“二叉”(binary),意思是在树中作区分。每个节点至多有两个子(child),left child & right child。二叉树在很多例子中使用,比如二叉树表示算术表达式。 如图:1/8是左节点;2/3是右节点; 二、二叉搜索树 BST 顾名思义,二叉树上又加了个搜索的限制。其要求:每个节点比其左子树元素大,比其右子树元素小。 如图:每个节点比它左子树的任意节点大,而且比它右子树的任意节点小 三、BST Java实现 直接上代码,对应代码分享在 Github 主页 BinarySearchTree.java package org.algorithm.tree; /* * Copyright [2015] [Jeff Lee] * * Licensed under the Apache License, Version 2.0 (the