[Programmer interview questions selected 100 questions] 4. All the paths in the binary tree and for a specific value, the programmer's Binary Tree

Source: Internet
Author: User

[Programmer interview questions selected 100 questions] 4. All the paths in the binary tree and for a specific value, the programmer's Binary Tree
[Question]

Enter an integer and a binary tree. Access from the root node of the tree to all the nodes that the leaf node passes through to form a path.

Print all paths equal to the input integer.

For example, enter the integer 22 and the following Binary Tree

10
/\
5 12
/\
4 7

The following two paths are printed: 10, 12, 10, 5, and 7.

[Analysis]

This is a written question by Baidu. It examines the understanding of the basic data structure and recursive functions of the tree.

When you access a node, add the node to the path and accumulate the value of the current node.

If the current node is a leaf node and the current path is exactly the same as the input integer, the current path meets the requirements and we print it out.

If the current node is not a leaf node, continue to access its child node. After the access to the current node ends, the recursive function automatically returns to the parent node.

Therefore, before exiting the function, we need to delete the current node in the path and subtract the value of the current node to ensure that the path to the returned parent node is exactly the path from the root node to the parent node.

It is not hard to see that the data structure of the stored path is actually a stack structure, because the path must be consistent with the recursive call state, and recursive call is essentially a process of pressure stack and exit stack.

[Code]

/********************************** Date: * Author: SJF0115 * Title: All paths in Binary Trees and for a specific value * Source: Baidu * category: classic interview questions *********************************/# include <iostream> # include <vector> using namespace std; struct TreeNode {int val; TreeNode * left; TreeNode * right; TreeNode (int x): val (x), left (NULL), right (NULL ){}}; // output path void PrintPath (vector <int> vec) {int len = vec. size (); for (int I = 0; I <len; I ++) {if (I! = Len-1) {cout <vec [I] <"->";} else {cout <vec [I] <endl ;} // if} // for // all the paths in the binary tree and all the paths for a specific value // root Binary Tree // The Return Path void FindBinaryTreePath (TreeNode * root, int sum, int & curSum, vector <int> & vec) {if (root = NULL) {return;} TreeNode * cur = root; // input stack vec. push_back (cur-> val); // current and curSum + = cur-> val; // whether the leaf node bool isLeaf = (cur-> left = NULL) & (cur-> right = NULL); // locate the path to the leaf node if (isLeaf & curSum = sum) {// output PrintPath (vec); return;} // if // left subtree if (cur-> left) {FindBinaryTreePath (cur-> left, sum, curSum, vec); // when we access this node, return to the parent node // Delete the node from the path and subtract the value curSum-= cur-> left-> val; vec from curSum. pop_back ();} // right subtree if (cur-> right) {FindBinaryTreePath (cur-> right, sum, curSum, vec ); // when we access this node, return to the parent node // Delete the node from the path and subtract the value curSum-= cur-> right-> val; vec from curSum. pop_back () ;}}// create a binary tree int CreateBTree (TreeNode * & T) {int data in the first sequence; // enter the value of the node in the binary tree in the first order, '-1' indicates the empty tree cin> data; if (data =-1) {T = NULL ;} else {// generate the root node T = new TreeNode (data); // construct the left subtree CreateBTree (T-> left ); // construct the right subtree CreateBTree (T-> right);} return 0;} int main () {int curSum = 0; vector <int> vec; treeNode * root (0); CreateBTree (root); FindBinaryTreePath (root, 22, curSum, vec); return 0 ;}


Test:

10 5 4-1-1 7-1-1 12-1-1

Output:

10-> 5-> 7

10-> 12


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.