1. Recover Binary Search Tree
Two elements of a binary search tree (BST) are 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 on how binary tree is 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 exists below.
Here's an example:
1 / \ 2 3 / 4 \ 5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}"
.
题意二叉查找树不合法,有两个节点的值被交换了,找出这两个节点并且不改变树的结构,使得二叉查找树合法,常数空间限制。
这题的要点就是想到使用树的递归中序遍历,因为二叉查找树合法的情况,中序遍历的值是从小到大排列的。
当出现当前值比前一个值小的时候,就是存在不合法的节点。
用pre存中序遍历时当前节点的前一个节点,方便值的大小对比,用s1,s2记录这两个不合法序列的位置,s1存较大的值,s2存较小的值。
最后把两个不合法的值交换。
- 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
.
有最大和的路径有一下三种情况:
1. 只有一个节点
2. 根节点+左子树
3. 根节点+右子树
4. 根节点+左子树+右子树
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
only, 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
.
问题是要求出 所有从根节点到叶子节点的路径和 的总和
这题与上一题类似,不同之处是需要添加变量记录之前的路径和
- 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 that 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 / / \ 11 13 4 / \ \ 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
每当计算到叶子节点时,比较是否与目标值相同
- 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);
- }
- //第二种写法
- 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 / / \ 11 13 4 / \ / \ 7 2 5 1
return
[ [5,4,11,2], [5,8,4,5] ]
- 二叉树递归,计算每条Path的和并比较,可以与前面几道题比较找到灵感
- void GetsumPath(vector<vector<int>> &result,
- vector<int> &path,
- int target,
- 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;
- }
Given a binary tree
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1 / \ 2 3 / \ / \ 4 5 6 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
从根节点一层递归连接left---right到最后一层,
处理完当前层后处理下一层
- 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 for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1 / \ 2 3 / \ \ 4 5 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
思路,与上一题类似,这里要多考虑如何找到下一个有效节点,需要先连接右边节点
- 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);
- }