二叉树遍历
typedef struct TreeNode
{
int data;
TreeNode * left;
TreeNode * right;
TreeNode * parent;
}TreeNode;
void pre_order(TreeNode * Node)
{
if(Node != NULL)
{
printf("%d ", Node->data);
pre_order(Node->left);
pre_order(Node->right);
}
}
void middle_order(TreeNode *Node) {
if(Node != NULL) {
middle_order(Node->left);
printf("%d ", Node->data);
middle_order(Node->right);
}
}
void after_order(TreeNode *Node) {
if(Node != NULL) {
after_order(Node->left);
after_order(Node->right);
printf("%d ", Node->data);
}
}
验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree
(BST).Assume a BST is defined as follows:
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 the node’s key. Both the left and right subtrees must
also be binary search trees.Example 1:
2
/ \
1 3
Input: [2,1,3] Output: true
- 算法一:中序遍历这棵树,如果是二叉搜索树,结果应该为升序。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 {
vector<int> vec;
int flag = 1;
public:
bool isValidBST(TreeNode* root) {
middle_order(root);
return flag;
}
void middle_order(TreeNode *Node) {
if(Node != NULL) {
middle_order(Node->left);
if(vec.size() == 0||Node->val > vec.back()){
vec.push_back(Node->val);
}else {flag = 0;}
middle_order(Node->right);
}
}
};
我这个做法里面有flag,不知道好不好,执行用时 :16 ms, 在所有 C++ 提交中击败了77.06%的用户内存消耗 :21.5 MB, 在所有 C++ 提交中击败了5.16%的用户。内存消耗的有点多。应该有改进的地方。
copy一个更好的写法,24 ms, 20.6 MB ,代码简洁一点。
class Solution {
public:
long x = LONG_MIN;
bool isValidBST(TreeNode* root) {
if(root == NULL){
return true;
}
else{
if(isValidBST(root->left)){
if(x < root->val){
x = root->val;
return isValidBST(root->right);
}
}
}
return false;
}
};
- 算法二:递归(copy的代码)
class Solution {
public:
bool isValidBST(TreeNode* root) {
return helper(root,LONG_MIN,LONG_MAX);
}
bool helper(TreeNode* cur,long lt,long rt){
if(cur==NULL) return true;
if(cur->val<=lt||cur->val>=rt) return false;
if(helper(cur->left,lt,cur->val)&&helper(cur->right,cur->val,rt)) return true;
return false;
}
};
作者:24shi-01fen-_00_01
链接:https://leetcode-cn.com/problems/validate-binary-search-tree/solution/liang-chong-fang-fa-di-gui-zhong-xu-bian-li-by-24s/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor
(LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common
ancestor is defined between two nodes p and q as the lowest node in T
that has both p and q as descendants (where we allow a node to be a
descendant of itself).”Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]
Example 1:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.扫描二维码关注公众号,回复: 8571663 查看本文章
以节点3和0为例
- 算法一:如果有parent指针,3和0第一个相同的parent节点即为所求
- 算法二:从根节点遍历到0和3,记录他们的路径。如620、6243,第一个分叉的节点2即为所求。O(N)
- 算法三:递归O(N):注意这个是二叉搜索树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
if(root==NULL) return NULL;
if(root==p||root==q) return root;//找到了
if(root->val>p->val&&root->val>q->val) return lowestCommonAncestor(root->left,p,q);
if(root->val<p->val&&root->val<q->val) return lowestCommonAncestor(root->right,p,q);
return root;
}