c++ 二叉树打印节点路径

leetcode上有道题是关于path sum的,我第一时间的做法是用二叉树的路径去与sum比较。所以需要去打印出二叉树的节点路径,以下是用回溯法。

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 
 5 struct TreeNode {
 6     int val;
 7     TreeNode *left;
 8     TreeNode *right;
 9     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10     
11 };
12 class Solution {
13 public:
14     bool hasPathSum(TreeNode* root, int sum) {
15         vector<int> path;
16         path.reserve(100);
17         bool flag = false;
18         printPaths(root, path, 0);
19         return flag;
20     }
21     void printPaths(TreeNode* node, vector<int>& path, int pathlen) {
22         if (node == NULL) return;
23         // append this node to the path array 
24         path[pathlen++] = node->val;
25         // path.push_back(node->val);
26         // it's a leaf, so print the path that led to here  
27         if (node->left == NULL && node->right == NULL) {
28             printArray(path, pathlen);
29         }
30         else {
31             // otherwise try both subtrees  
32             printPaths(node->left, path, pathlen);
33             printPaths(node->right, path, pathlen);
34             path.pop_back();
35         }
36     }
37     void printArray(vector<int> &ints, int len) {
38         for (int i = 0; i < len; i++) {
39              cout << ints[i] << " " ; 
40         }
41          cout << endl;
42     }
43 };
44 
45 int main()
46 {
47     Solution res;
48     TreeNode t1(5);
49     TreeNode t2(4);
50     TreeNode t3(8);
51     TreeNode t4(11);
52     TreeNode t5(13);
53     TreeNode t6(4);
54     TreeNode t7(7);
55     TreeNode t8(2);
56     TreeNode t9(1);
57     t1.left = &t2;
58     t1.right = &t3;
59     t2.left = &t4;
60     t3.left = &t5;
61     t3.right = &t6;
62     t4.left = &t7;
63     t4.right = &t8;
64     t6.right = &t9;
65     TreeNode * root = &t1;
66     bool ret = res.hasPathSum(root,22);
67     
68     system("pause");
69     return 0;
70 }

接着与sum做比较就能知道path sum是否相同了。

 1 class Solution {
 2 public:
 3     bool hasPathSum(TreeNode* root, int sum) {
 4         vector<int> path;
 5         path.reserve(100);
 6         bool flag = false;
 7         printPaths(root, path, 0, sum, flag);
 8         return flag;
 9     }
10     void printPaths(TreeNode* node, vector<int>& path, int pathlen, int& sum, bool& flag) {
11         if (node == NULL) return;
12         // append this node to the path array 
13         path[pathlen++] = node->val;
14         // path.push_back(node->val);
15         // it's a leaf, so print the path that led to here  
16         if (node->left == NULL && node->right == NULL) {
17             printArray(path, pathlen, sum, flag);
18             if (flag) return;
19         }
20         else {
21             // otherwise try both subtrees  
22             printPaths(node->left, path, pathlen, sum, flag);
23             printPaths(node->right, path, pathlen, sum, flag);
24             path.pop_back();
25         }
26     }
27     void printArray(vector<int> &ints, int len, int& sum, bool& flag) {
28         int num = 0;
29         for (int i = 0; i < len; i++) {
30             // cout << ints[i] << " " ; 
31             num += ints[i];
32         }
33         if (num == sum) flag = true;
34         // cout << endl;
35     }
36 };

不过这种方法不好,空间开销大,效率还不高。在网上看到这种解法贴一下地址:

 点击

转载于:https://www.cnblogs.com/Hwangzhiyoung/p/10364254.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
叉树的后序遍历是先遍历左子树,再遍历右子树,最后遍历根节点。因此,在搜索节点时也需要先递归遍历左子树和右子树,最后才能判断当前节点是否为目标节点。 以下是一个示例代码: ```c++ #include<iostream> #include<vector> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; bool postorderTraversal(TreeNode* root, int target, vector<int>& path) { if (root == NULL) return false; // 当前节点为空,返回false bool found = false; if (root->left) found = postorderTraversal(root->left, target, path); // 递归遍历左子树 if (root->right) found = postorderTraversal(root->right, target, path) || found; // 递归遍历右子树 if (root->val == target) found = true; // 当前节点为目标节点,将found设置为true if (found) path.push_back(root->val); // 如果找到目标节点,则将当前节点的值添加到路径中 return found; } int main() { // 构造一棵二叉树 TreeNode* root = new TreeNode(5); root->left = new TreeNode(3); root->right = new TreeNode(8); root->left->left = new TreeNode(2); root->left->right = new TreeNode(4); root->right->left = new TreeNode(7); root->right->right = new TreeNode(9); int target = 7; vector<int> path; bool found = postorderTraversal(root, target, path); if (found) { cout << "Path to node " << target << ": "; for (int i = path.size() - 1; i >= 0; i--) { cout << path[i] << " "; } cout << endl; } else { cout << "Node " << target << " not found." << endl; } return 0; } ``` 在以上代码中,`postorderTraversal`函数返回一个bool值,表示是否找到目标节点。如果找到,则将路径上的节点值添加到`path`向量中。最后,根据`path`向量输出路径
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值