Skip to content

Instantly share code, notes, and snippets.

@gcrfelix
Last active January 22, 2017 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcrfelix/875f4c7fb1ff8be9807e to your computer and use it in GitHub Desktop.
Save gcrfelix/875f4c7fb1ff8be9807e to your computer and use it in GitHub Desktop.
题目: find Longest Consective Increasing Sequence in Binary Tree from root
import java.util.ArrayList;
import java.util.List;
public class LICSinBinaryTree {
public int getLICSinBT(TreeNode root) {
if(root == null) {
return 0;
}
int[] max = new int[1];
helper(root, Integer.MIN_VALUE, 0, max);
return max[0];
}
public void helper(TreeNode cur, int pre, int count, int[] max) {
if(cur == null || cur.val <= pre.val) {
max[0] = Math.max(max[0], count);
return;
}
helper(cur.left, cur.val, count+1, max);
helper(cur.right, cur.val, count+1, max);
}
public static void main(String[] args) {
LICSinBinaryTree tree = new LICSinBinaryTree();
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
TreeNode n6 = new TreeNode(6);
TreeNode n7 = new TreeNode(7);
n2.left = n1;
n2.right = n5;
n5.left = n3;
n5.right = n6;
n6.right = n7;
// n3.right = n7;
System.out.println(tree.getLICSinBT(n2));
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val) {
this.val = val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment