1. Recover Binary Search Tree
Elements of a binary search tree (BST) is swapped by mistake.
Recover the tree without changing its structure. Note:
A solution using O (n) space is pretty straight forward. Could you devise a constant space solution?
Confused what "{1,#,2,3}" means? > read more about how binary tree was serialized on OJ.
OJ ' s Binary Tree serialization:
The serialization of a binary tree follows a level order traversal, where ' # ' signifies a path terminator where no node ex Ists below.
Here's an example:
1
/\
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
Test instructions Binary lookup tree is not legal, there are two node values are exchanged, find the two nodes and do not change the structure of the tree, so that the binary lookup tree is legal, constant space constraints.
The main point of this problem is to think of using the recursive middle sequence traversal of the tree, because the binary search tree is a valid case, the value of the middle sequence traversal is from small to large arrangement.
When the current value is smaller than the previous value, there is an illegal node.
With the pre-stored in the sequence of the current node in the previous node, the convenient value of the size of the comparison, with S1,S2 record the location of the two illegal sequences, s1 large values, s2 save a smaller value.
Finally, the two illegal values are exchanged.
void Gettwonode (TreeNode *root,treenode *&n1, treenode*&n2, treenode*&pre) {
if (!root) return;
Gettwonode (Root->left, N1, N2, pre);
if (pre && pre->val > Root->val) {
n2 = root;
if (n1==null)
n1 = pre;
}
Pre = root;
Gettwonode (Root->right,n1,n2,pre);
}
void Recovertree (TreeNode *root) {
TreeNode *n1=null, *n2=null, *pre = NULL;
if (!root) return;
Gettwonode (root,n1,n2,pre);
if (N1 && n2) {
int temp = n1->val;
N1->val = n2->val;
N2->val = temp;
}
}
2. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1
/\
2 3
Return 6. The topic requires finding the largest and most large path in a two-fork tree
There are three scenarios where the maximum and the path are:
1. Only one node
2. root node + Zuozi
3. root node + Right sub-tree
4. root node + Left sub-tree + right subtree
Trace the four path and pick up the max path
int Getmax (TreeNode *root, int &maxcrossroot) {
if (root = NULL) return 0;
int left = Getmax (Root->left, maxcrossroot);
int right = Getmax (Root->right, maxcrossroot);
int rMax = root->val;
if (left>0)
RMax + = left;
if (right>0)
rMax + = right;
Maxcrossroot = Std::max (Maxcrossroot, rMax);
Return Std::max (Root->val, Std::max (Root->val+left, Root->val+right));
}
int Maxpathsum (TreeNode *root) {
int maxcrossroot = int_min;
int maxendbyroot = Getmax (root, maxcrossroot);
Return Std::max (Maxendbyroot,maxcrossroot);
}
3. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 to, each root-to-leaf path could represent a number.
An example is the Root-to-leaf path 1->2->3 which represents the number 123.
Find The total sum of all root-to-leaf numbers.
For example,
1
/\
2 3
The Root-to-leaf path 1->2 represents the number 12.
The Root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.
The problem is that all paths and sums from the root node to the leaf node are required.
This is similar to the previous question, and the difference is that you need to add the path before the variable record and
void Sumsubpath (treenode* root, int &sum, int path) {
if (root = NULL) return;
Path = path*10+ root->val;
if (Root->left = = NULL && Root->right = = null) {
sum + = path;
return;
}
Sumsubpath (root->left, sum, path);
Sumsubpath (root->right, sum, path);
}
int sumnumbers (TreeNode *root) {
int sum = 0;
int path = 0;
Sumsubpath (Root,sum,path);
return sum;
}
4. Path sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such this adding up all the values along the Path equals the given sum. For example:
Given the below binary tree and sum = 22,
5
/\
4 8
/ /\
all 4
/ \ \
7 2 1
Return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
Whether the comparison is the same as the target value whenever a leaf node is calculated
void
Judgesubpath (treenode* root, int sum, int target, bool &result) {if (root = NULL) return;
Sum + = root->val;
if (Root->left = = NULL && Root->right = = null) {if (sum = = target) result = true;
Return
} judgesubpath (Root->left,sum,target,result);
Judgesubpath (Root->right,sum,target,result);
}//second notation bool Judgepathsum (treenode* root, int sum, int target) {if (root = NULL) return false;
Sum + = root->val;
if (Root->left = = NULL && Root->right = = null) {if (sum = = target) return true;
else return false; } return Judgepathsum (root->left,sum,target) | |
Judgepathsum (Root->right, sum,target);
} bool Haspathsum (TreeNode *root, int sum) {bool result = false;
Judgesubpath (Root,0,sum,result);
return result; }
5. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path ' s sum equals the Given sum. For example:
Given the below binary tree and sum = 22,
5
/\
4 8
/ /\
all 4
/ \ /\
7 2 5 1
Return
[
[5,4,11,2],
[5,8,4,5]
]
Binary tree recursion, calculate each path and compare, can be compared with the previous questions to find inspiration
void Getsumpath (vector<vector<int>> &result, vector<int> &path, int t
arget, int sum, TreeNode *root) {if (root = NULL) return;
Sum + = root->val;
Path.push_back (Root->val);
if (target = = Sum && Root->left = = NULL && Root->right = = null) {result.push_back (path);
}else{if (root->left! = NULL) Getsumpath (result,path,target,sum,root->left);
if (root->right! = NULL) Getsumpath (result,path,target,sum,root->right);
} path.pop_back ();
Sum-= root->val;
Return
} vector<vector<int> > Pathsum (TreeNode *root, int sum) {vector<vector<int>> result;
Vector<int> path;
Getsumpath (Result,path,sum,0,root);
return result; }
6. Populating Next right pointers in each Node
Given a binary tree
struct Treelinknode {
treelinknode *left;
Treelinknode *right;
Treelinknode *next;
}
Populate each of the next pointer to the next right node. If There is no next right node, the next pointer should are set to NULL.
Initially, all next pointers is set to NULL.
NOTE:YOU constant extra space. You could assume that it was a perfect binary tree (ie, all leaves was at the same level, and every parent had both children).
For example,
Given the following perfect binary tree,
1
/ \
2 3
/\ /\
4 5 6 7
After calling your function, the tree is should look like:
1, null
/ \
2, 3, null
/\ /\
4->5->6->7, NULL
Recursively connect the left---right to the last layer from the root node,
Process the next layer after processing the current layer
void Connect (Treelinknode *root) {
if (root = NULL) return;
if (root->left! = NULL)
root->left->next = root->right;
if (root->right! = NULL)
root->right->next = root->next? root->next->left:null;
Connect (root->left);
Connect (root->right);
}
7.
populating Next right pointers in each Node II
Follow up to problem "populating Next right pointers in each Node".
What if the given tree could is any binary tree? Would your previous solution still work?
NOTE:YOU constant extra space.
For example,
Given The following binary tree,
1
/ \
2 3
/\ \
4 5 7
After calling your function, the tree is should look like:
1, null
/ \
2, 3, null
/\ \
4-> 5, 7, NULL
Ideas, similar to the previous question, here to consider how to find the next effective node, you need to first connect to the right node
void Connectanybt (Treelinknode *root) {
if (root = NULL) return;
Treelinknode *p = root->next;
while (P! = null) {
if (p->left! = null) {
p = p->left;
break;
}
if (p->right! = NULL) {
p = p->right;
break;
}
p = p->next;
}
if (root->right! = NULL) {
root->right->next = p;
}
if (root->left! = NULL) {
Root->left->next = root->right? root->right:p;
}
CONNECTANYBT (root->right);
CONNECTANYBT (Root->left);
}