本文最后更新于:2022年9月12日 晚上
给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
输入: 1 / \ 2 3 \ 5 输出: ["1->2->5" , "1->3" ] 解释: 所有根节点到叶子节点的路径为: 1 ->2 ->5 , 1 ->3
Solution
参考 代码随想录 ,分析回溯过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 class Solution {private : void traversal (TreeNode* cur, vector <int >& path, vector <string >& result) { path.push_back(cur->val); if (cur->left == NULL && cur->right == NULL ) { string sPath; for (int i = 0 ; i < path.size() - 1 ; i++) { sPath += to_string(path[i]); sPath += "->" ; } sPath += to_string(path[path.size() - 1 ]); result.push_back(sPath); return ; } if (cur->left) { traversal(cur->left, path, result); path.pop_back(); } if (cur->right) { traversal(cur->right, path, result); path.pop_back(); } }public : vector <string > binaryTreePaths (TreeNode* root) { vector <string > result; vector <int > path; if (root == NULL ) return result; traversal(root, path, result); return result; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 class Solution {public : vector <string > binaryTreePaths (TreeNode* root) { vector <string > res; if (root==nullptr ) return res; if (root->left==nullptr && root->right==nullptr ){ res.push_back(to_string(root->val)); return res; } vector <string > leftPaths = binaryTreePaths(root->left); for (int i=0 ; i<leftPaths.size(); ++i){ res.push_back(to_string(root->val)+"->" +leftPaths[i]); } vector <string > rightPaths = binaryTreePaths(root->right); for (int i=0 ; i<rightPaths.size(); ++i){ res.push_back(to_string(root->val)+"->" +rightPaths[i]); } return res; } };
迭代法,广度优先搜索 参考**@LeetCode官方**
维护两个队列,存储节点和根到该节点的路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 class Solution {public : vector <string > binaryTreePaths (TreeNode* root) { vector <string > res; if (root==nullptr ) return res; queue <TreeNode *> qNode; queue <string > qPath; qNode.push(root); qPath.push(to_string(root->val)); while (!qNode.empty()){ TreeNode* node = qNode.front(); string path = qPath.front(); qNode.pop(); qPath.pop(); if (node->left==nullptr && node->right==nullptr ){ res.push_back(path); } else { if (node->left){ qNode.push(node->left); qPath.push(path+"->" +to_string(node->left->val)); } if (node->right){ qNode.push(node->right); qPath.push(path+"->" +to_string(node->right->val)); } } } return res; } };
java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 class Solution { public List<String> binaryTreePaths (TreeNode root) { List<String> result = new ArrayList<>(); if (root == null ) { return result; } List<Integer> path = new ArrayList<>(); traversal(root, path, result); return result; } private void traversal (TreeNode cur, List<Integer> path, List<String> result) { path.add(cur.val); if (cur.left == null && cur.right == null ) { StringBuilder sb = new StringBuilder(); for (int i = 0 ; i < path.size() - 1 ; ++i) { sb.append(path.get(i)).append("->" ); } sb.append(path.get(path.size() - 1 )); result.add(sb.toString()); } if (cur.left != null ) { traversal(cur.left, path, result); path.remove(path.size() - 1 ); } if (cur.right != null ) { traversal(cur.right, path, result); path.remove(path.size() - 1 ); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Solution { public List<String> binaryTreePaths (TreeNode root) { List<String> result = new ArrayList<>(); if (root == null ) { return result; } Queue<TreeNode> nodeQueue = new LinkedList<>(); Queue<String> pathQueue = new LinkedList<>(); nodeQueue.offer(root); pathQueue.offer(Integer.toString(root.val)); while (!nodeQueue.isEmpty()) { TreeNode node = nodeQueue.poll(); String path = pathQueue.poll(); if (node.left == null && node.right == null ) { result.add(path); } else { if (node.left != null ) { nodeQueue.offer(node.left); pathQueue.offer(new StringBuffer(path).append("->" ) .append(node.left.val).toString()); } if (node.right != null ) { nodeQueue.offer(node.right); pathQueue.offer(new StringBuffer(path).append("->" ) .append(node.right.val).toString()); } } } return result; } }