257. 二叉樹的所有路徑
阿新 • • 發佈:2021-12-08
題目連結:
題目描述
給你一個二叉樹的根節點 root
,按 任意順序 ,返回所有從根節點到葉子節點的路徑。
葉子節點 是指沒有子節點的節點。
示例 1:
輸入:root = [1,2,3,null,5]
輸出:["1->2->5","1->3"]
示例 2:
輸入:root = [1]
輸出:["1"]
題解
思路:該題求根節點到葉子節點的路徑,所以採用前序遍歷(父節點指向孩子節點)。這題涉及到回溯。前序遍歷及其回溯過程如下:
方法一:遞迴
程式碼(C++)
//遞迴:前序遍歷 class Solution { public: void traversal(TreeNode* node, vector<int> &path, vector<string> &result){ path.push_back(node->val);//中 if (node->left == nullptr && node->right == nullptr) { string spath = ""; for(int i = 0; i < path.size() - 1; i++) { spath+= to_string(path[i]) + "->"; } spath += to_string(path[path.size() - 1]); result.push_back(spath); return; } if (node->left) { //左 traversal(node->left, path, result); path.pop_back(); //回溯 }if (node->right) { //右 traversal(node->right, path, result); path.pop_back(); //回溯 } } vector<string> binaryTreePaths(TreeNode* root) { vector<string> result; vector<int> path; if (root == nullptr) return result; traversal(root, path, result); return result; } };
程式碼(JavaScript)
//遞迴:前序遍歷 function getPath(node, path, result) { path.push(node.val); //中 if (node.left === null && node.right === null) { var spath = ""; for (var i = 0; i < path.length - 1; i++) { spath += path[i] + "->"; } spath += path[path.length - 1]; result.push(spath); return; } if (node.left) { //左 getPath(node.left, path, result); path.pop(); //回溯 } if (node.right) { //右 getPath(node.right, path, result); path.pop(); //回溯 } } var binaryTreePaths = function(root) { let result = []; let path = []; if (root == null) return result; getPath(root, path, result); return result; };
方法二:迭代
程式碼(C++)
//迭代:前序遍歷 class Solution { public: vector<string> binaryTreePaths(TreeNode* root) { vector<string> result; //最終的路徑集合 stack<TreeNode*> treeSt; //存放遍歷的節點的棧 stack<string> pathSt; //存放臨時路徑的棧 if (root == nullptr) return result; else { treeSt.push(root); pathSt.push(to_string(root->val)); } while (!treeSt.empty()) { TreeNode* node = treeSt.top(); if (node != nullptr) { string spath = pathSt.top(); treeSt.pop(); pathSt.pop(); if (node->right == nullptr && node->left == nullptr) { result.push_back(spath); } if (node->right) {//右 treeSt.push(node->right); pathSt.push(spath + "->" + to_string(node->right->val)); } if (node->left) {//左 treeSt.push(node->left); pathSt.push(spath + "->" + to_string(node->left->val)); } treeSt.push(node); //中 treeSt.push(nullptr); //null } else { treeSt.pop(); treeSt.pop(); } } return result; } };
程式碼(JavaScript)
//迭代:統一前序遍歷(右左中null) var binaryTreePaths = function(root) { let pathSt = []; //存放臨時路徑的棧 let treeSt = []; //存放遍歷的節點的棧 let result = []; //最終的路徑集合 if (root === null) return result; else { treeSt.push(root); pathSt.push(root.val + ""); // + "" 是讓root.val 轉成 字串 } while (treeSt.length != 0) { var node = treeSt.pop(); if (node != null) { var spath = pathSt.pop(); if (node.left === null && node.right === null) { result.push(spath); } if (node.right) { //右 treeSt.push(node.right); pathSt.push(spath + "->" + node.right.val); } if (node.left) { //左 treeSt.push(node.left); pathSt.push(spath + "->" + node.left.val); } treeSt.push(node); //中 treeSt.push(null); //null } else { treeSt.pop(); } } return result; };