Advertisement
1988coder

114. Flatten Binary Tree to Linked List

Feb 4th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
  2.  
  3. // Definition for a binary tree node.
  4. class TreeNode {
  5.     int val;
  6.     TreeNode left;
  7.     TreeNode right;
  8.  
  9.     TreeNode(int x) {
  10.         val = x;
  11.     }
  12. }
  13.  
  14. /**
  15.  * Iterative Solution
  16.  *
  17.  * Refer:
  18.  * https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/37010/Share-my-simple-NON-recursive-solution-O(1)-space-complexity!
  19.  *
  20.  * Time Complexity: O(N)
  21.  *
  22.  * Space Complexity: O(1)
  23.  *
  24.  * N = Number of nodes in the tree.
  25.  */
  26. class Solution1 {
  27.     public void flatten(TreeNode root) {
  28.         if (root == null) {
  29.             return;
  30.         }
  31.  
  32.         TreeNode cur = root;
  33.  
  34.         while (cur != null) {
  35.             // If there is no left node, the current node is at correct place in the tree.
  36.             if (cur.left != null) {
  37.  
  38.                 // We are trying to find left subTree's rightmost child. This node's right child
  39.                 // will be the current right node.
  40.                 TreeNode pre = cur.left;
  41.                 while (pre.right != null) {
  42.                     pre = pre.right;
  43.                 }
  44.                 pre.right = cur.right;
  45.  
  46.                 // Since the right subtree has been place at the right place, Left subtree can
  47.                 // move to the right now.
  48.                 cur.right = cur.left;
  49.                 cur.left = null;
  50.             }
  51.             cur = cur.right;
  52.         }
  53.     }
  54. }
  55.  
  56. /**
  57.  * Recursive Solution
  58.  *
  59.  * Refer:
  60.  * https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/37010/Share-my-simple-NON-recursive-solution-O(1)-space-complexity!
  61.  *
  62.  * Time Complexity: O(N)
  63.  *
  64.  * Space Complexity: O(Height of the tree). In worst case O(N)
  65.  *
  66.  * N = Number of nodes in the tree.
  67.  */
  68. class Solution2 {
  69.     public void flatten(TreeNode root) {
  70.         if (root == null) {
  71.             return;
  72.         }
  73.         flattenHelper(root, null);
  74.     }
  75.  
  76.     // head node represents the head of the solved list.
  77.     private TreeNode flattenHelper(TreeNode node, TreeNode head) {
  78.         if (node == null) {
  79.             return head;
  80.         }
  81.  
  82.         head = flattenHelper(node.right, head);
  83.         head = flattenHelper(node.left, head);
  84.         node.right = head;
  85.         node.left = null;
  86.         return node;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement