LeetCode – Refresh – Binary Tree Upside Down

Recursive method can be unstand easily:

1. Assume we get the sub node with this transition. So we need to make the current node.

2. As the symmetic, the finished sub node will replace the position of current node.

3. old current->left = parent ->right, old current->right = parent, since we passed the these from argument. (one corner case : if parent == NULL, there is no parent->right)

This is a button up recursive:

 1 /**
 2  * Definition for binary tree
 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 *getTree(TreeNode *root, TreeNode *parent) {
13         if (!root) return parent; //Find the most left, but root is NULL, so the new root is parent.
14         TreeNode *current = getTree(root->left, root); //This is sub node, but after conversion, it replace the current node.
15         root->left = parent == NULL ? NULL : parent->right;
16         root->right = parent;
17         return current;
18     }
19     TreeNode *upsideDownBinaryTree(TreeNode *root) {
20         return getTree(root, NULL);
21     }
22 };

Here‘s the top down iterative:

 1 /**
 2  * Definition for binary tree
 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) return root;
14      TreeNode *parent = NULL, *parentRight = NULL;
15      while (root) {
16          TreeNode *lnode = root->left;
17          root->left = parentRight;
18          parentRight = root->right;
19          root->right = parent;
20          parent = root;
21          root = lnode;
22      }
23      return parent;
24     }
25 };
时间: 2024-10-19 00:10:13

LeetCode – Refresh – Binary Tree Upside Down的相关文章

【LeetCode】Binary Tree Upside Down

Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into

[LeetCode#156] Binary Tree Upside Down

Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf node

[leetcode]156.Binary Tree Upside Down颠倒二叉树

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return

[LeetCode] 156. Binary Tree Upside Down 二叉树的上下颠倒

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return

LeetCode – Refresh – Binary Tree Iterator

It is similar to use stack for BST inorder traversal. So do the same thing : 1. Get stack to store right branchs (include current node). 2. When you pop the top node, remember to push all the right sub branches again. Otherwise, it will cause right's

LeetCode – Refresh – Binary Tree Inorder Traversal

There are three methods to do it: 1. recursive(use memory stack): (Time O(n), Space O(logn) 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), rig

LeetCode – Refresh – Binary Tree Post Order Traversal

Still 3 methods: Same with inorder, but post order is a little different. We need to treat it as reverse result of preorder. So we go to right branch first, then go back to left branch 1. recursive (use memory stack); 1 /** 2 * Definition for binary

LeetCode – Refresh – Binary Tree Level Order Traversal

Basic question. Use a queue to store Node, use two numbers to record current level node numbers and next level node numbers. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode

LeetCode – Refresh – Binary Tree Pre Order Traversal

Same with inorder. 1. recursive: 1 /** 2 * Definition for binary tree 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 v