Given a binary tree, find the length of the longest consecutive sequence path.
The path refers to any sequence of nodes from some starting node to all node in the tree along the Parent-child connection S. The longest consecutive path need to is from parent to child (cannot be the reverse).
For example,
1 3 / 2 4 5
Longest consecutive sequence path 3-4-5
is, so return 3
.
2 3 / 2 / 1
Longest consecutive sequence path 2-3
is, not 3-2-1
, so return 2
.
This problem allows us to find the longest continuous sequence of binary trees, about binary tree problem basically all need to traverse the tree, and recursive traversal writing is particularly simple, the following method is used to recursive version of the first order traversal, we for each traversed to the node, we see whether the node value is greater than the value of the parameter (parent node value) 1, if it is the length plus 1, Otherwise the length is reset to 1, then the result res is updated, and then the left and right child nodes are recursively called, see the code below:
Solution One:
classSolution { Public: intLongestconsecutive (treenode*root) { if(!root)return 0; intres =0; Dfs (root, root->val-1,0, RES); returnRes; } voidDFS (TreeNode *root,intVint out,int&Res) { if(!root)return; if(Root->val = = v +1) ++ out; Else out=1; Res= Max (res, out); DFS (Root->left, Root->val, out, RES); DFS (Root->right, Root->val, out, RES); }};
The following method of writing is to use the idea of divide and conquer, the left and right child nodes are processed separately, if the left dial hand node exists and the node value than its parent node value 1, then recursive call function, if the node value is not exactly 1, then recursive call reset the length of the function, for the right child node processing and left child node is the same, see
Solution Two:
classSolution { Public: intLongestconsecutive (treenode*root) { if(!root)return 0; intres =0; DFS (Root,1, RES); returnRes; } voidDFS (TreeNode *root,intLenint&Res) {Res=Max (res, Len); if(root->Left ) { if(Root->left->val = = Root->val +1) DFS (Root->left, Len +1, RES); ElseDFS (Root->left,1, RES); } if(root->Right ) { if(Root->right->val = = Root->val +1) DFS (root->right, Len +1, RES); ElseDFS (Root->right,1, RES); } }};
The following recursive notation is quite concise, but the core idea is not much different from the above two methods, see the code below:
Solution Three:
classSolution { Public: intLongestconsecutive (treenode*root) { returnHelper (root, NULL,0); } intHelper (TreeNode *root, TreeNode *p,intRes) { if(!root)returnRes; Res= (P && root->val = = P->val +1) ? Res +1:1; returnMax (res, max (helper (root->left, Root, res), helper (root->Right , Root, res)); }};
The above three kinds are recursive writing, the following we take a look at the iterative method, the wording is slightly more complex, with the idea of Dfs, with the sequence to traverse the tree, for the traversal of the node, we see its left and right sub-node has not satisfied test instructions, if the left dial hand node is larger than its parent node 1, if the child node exists, then , the pointer moves to the left child node, whereas if the right child node is 1 larger than its parent node, if the left child node is present, the queue is queued, the pointer moves to the right child node, and so on until the queue is empty, see the code below:
Solution Four:
classSolution { Public: intLongestconsecutive (treenode*root) { if(!root)return 0; intres =0; Queue<TreeNode*>Q; Q.push (root); while(!Q.empty ()) { intLen =1; TreeNode*t =Q.front (); Q.pop (); while((t->left && T->left->val = = T->val +1) || (t->right && T->right->val = = T->val +1)) { if(t->left && T->left->val = = T->val +1) { if(t->right) Q.push (t->Right ); T= t->Left ; } Else if(t->right && T->right->val = = T->val +1) { if(t->left) Q.push (t->Left ); T= t->Right ; } ++Len; } if(t->left) Q.push (t->Left ); if(t->right) Q.push (t->Right ); Res=Max (res, Len); } returnRes; }};
Similar topics:
Longest increasing subsequence
Resources:
Https://leetcode.com/discuss/67938/my-c-4-lines-recursive-solution-40ms
Https://leetcode.com/discuss/86742/ac-java-iterative-solution-using-queue
Https://leetcode.com/discuss/85086/15-lines-concise-and-easy-understand-c-solution
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Binary tree longest consecutive Sequence the longest continuous sequence of two forks