Advertisement
1988coder

298. Binary Tree Longest Consecutive Sequence

Dec 2nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * public class TreeNode {
  4.  *     int val;
  5.  *     TreeNode left;
  6.  *     TreeNode right;
  7.  *     TreeNode(int x) { val = x; }
  8.  * }
  9.  */
  10. /*
  11. Top Down DFS Approach
  12.  
  13. Time Complexity: O(N)
  14. Space Complexity: O(H)
  15.  
  16. N = Number of nodes in the binary tree.
  17. H = Height of the binary Tree. This will be equal to N if the tree is skewed.
  18. */
  19. class Solution {
  20.     public int longestConsecutive(TreeNode root) {
  21.         if (root == null) {
  22.             return 0;
  23.         }
  24.         return dfsHelper(root, root.val, 0);
  25.     }
  26.    
  27.     public int dfsHelper(TreeNode node, int target, int curLength) {
  28.         if (node == null) {
  29.             return curLength;
  30.         }
  31.         curLength = node.val == target ? curLength+1 : 1;
  32.         return Math.max(curLength, Math.max(dfsHelper(node.left, node.val+1, curLength), dfsHelper(node.right, node.val+1, curLength)));
  33.     }
  34. }
  35.  
  36. /*
  37. Iterative Approach
  38.  
  39. Time Complexity: O(N)
  40. Space Complexity: O(H)
  41.  
  42. N = Number of Nodes in the binary tree.
  43. H = Height of the binary Tree. This will be equa to N if the tree is skewed.
  44. */
  45. import javafx.util.Pair;
  46. class Solution {
  47.     public int longestConsecutive(TreeNode root) {
  48.         if (root == null) {
  49.             return 0;
  50.         }
  51.         int maxLength = 0;
  52.         Stack<Pair<TreeNode, Integer>> stack = new Stack();
  53.         stack.push(new Pair<TreeNode, Integer>(root, 1));
  54.         while (!stack.isEmpty()) {
  55.             Pair<TreeNode, Integer> pair = stack.pop();
  56.             TreeNode curNode = pair.getKey();
  57.             int curLength = pair.getValue();
  58.             maxLength = Math.max(maxLength, curLength);
  59.             if (curNode.right != null) {
  60.                 if (curNode.right.val == curNode.val+1) {
  61.                     stack.push(new Pair<TreeNode, Integer>(curNode.right, curLength+1));
  62.                 } else {
  63.                     stack.push(new Pair<TreeNode, Integer>(curNode.right, 1));
  64.                 }
  65.             }
  66.             if (curNode.left != null) {
  67.                 if (curNode.left.val == curNode.val+1) {
  68.                     stack.push(new Pair<TreeNode, Integer>(curNode.left, curLength+1));
  69.                 } else {
  70.                     stack.push(new Pair<TreeNode, Integer>(curNode.left, 1));
  71.                 }
  72.             }
  73.         }
  74.         return maxLength;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement