1: /**
2: * Definition for a binary tree node.
3: * struct TreeNode {
4: * int val;
5: * TreeNode *left;
6: * TreeNode *right;
7: * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8: * };
9: */
10: class Solution {
11: public:
12: TreeNode* upsideDownBinaryTree(TreeNode* root) {
13: if (root == NULL) return root;
14: TreeNode *res = root;
15: while (res->left) res = res->left;
16: helper(root);
17: return res;
18: }
19: TreeNode *helper(TreeNode *root) {
20: if (root->left == NULL && root->right == NULL) return root;
21: TreeNode *nr = helper(root->left);
22: nr->left = root->right;
23: nr->right = root;
24: root->left = NULL;
25: root->right = NULL;
26: return root;
27: }
28: };
Saturday, August 13, 2016
156. Binary Tree Upside Down
Postorder traversal and then construct the tree from downside to upside.
Labels:
leetcode,
linkedin,
postorder traversal,
tree
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment