257. Binary Tree Paths(列印二叉樹所有路徑)
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
首先我採用了dfs,但是我的方法有點麻煩。
方法一:dfs
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(root == NULL)
return res;
string str = std::to_string(root->val);
if(root->left != NULL || root->right != NULL){
dfs(root->left, res, str);
dfs(root->right, res, str);
}
else
res.push_back(str);
return res;
}
void dfs(TreeNode* root, vector<string>& res, string str){
if(root == NULL) return;
str += "->" + std::to_string(root->val);
if(root->left == NULL && root->right == NULL){
res.push_back(str);
return ;
}
dfs(root->left, res, str);
dfs(root->right, res, str);
}
};
然後我參照別人的dfs,重新用Python寫了一遍,簡化了一些步驟。
下面是dfs的簡潔版本:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def binaryTreePaths(self, root):
if not root:
return []
res = []
self.dfs(root, res, "")
return res;
def dfs(self, root, res, ls):
if not root.left and not root.right:
res.append(ls+str(root.val));
return res
if root.left:
self.dfs(root.left, res, ls+str(root.val)+"->")
if root.right:
self.dfs(root.right, res, ls+str(root.val)+"->")
非遞迴版的dfs,類似於二叉樹的前序遍歷,使用stack:
class Solution(object):
def binaryTreePaths(self, root):
res = []
if not root:
return res
self.dfs(root, res)
return res;
def dfs(self, root, res):
if not root:
return []
stack = [(root, "")]
while stack:
node, ls = stack.pop()
if not node.left and not node.right:
res.append(ls+str(node.val))
if node.right:
stack.append((node.right, ls+str(node.val)+"->"))
if node.left:
stack.append((node.left, ls+str(node.val)+"->"))
return res
方法二:採用廣度優先BFS,使用佇列:
class Solution(object):
def binaryTreePaths(self, root):
res = []
if not root:
return res
self.bfs(root, res)
return res;
def bfs(self, root, res):
if not root:
return []
queue = collections.deque([(root, "")])
while queue:
node, ls = queue.popleft()
if not node.left and not node.right:
res.append(ls+str(node.val))
if node.left:
queue.append((node.left, ls+str(node.val)+"->"))
if node.right:
queue.append((node.right, ls+str(node.val)+"->"))
return res
相關文章
- 列印二叉樹的所有路徑二叉樹
- Leetcode Binary Tree PathsLeetCode
- 二叉樹和為某種所有路徑二叉樹
- LeetCode-Binary Tree PathsLeetCode
- Maximum Depth of Binary Tree 二叉樹的深度二叉樹
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉樹二叉樹
- 二叉搜尋樹(Binary Search Tree)(Java實現)Java
- iOS資料結構與演算法實戰 二叉樹的演算法實戰 Binary Tree PathsiOS資料結構演算法二叉樹
- 程式碼隨想錄演算法訓練營第十七天| 110. 平衡二叉樹 257. 二叉樹的所有路徑 404. 左葉子之和演算法二叉樹
- LeetCode 545. Boundary of Binary Tree 二叉樹邊界LeetCode二叉樹
- 二叉搜尋樹(Binary Search Tree )的定義及分析
- 如何在Java中實現二叉搜尋樹( binary search tree)?Java
- Binary Tree Traversals(HDU1710)二叉樹的簡單應用二叉樹
- 【LeetCode 144_二叉樹_遍歷】Binary Tree Preorder TraversalLeetCode二叉樹
- 【LeetCode 110_二叉樹_遍歷】Balanced Binary TreeLeetCode二叉樹
- 【LeetCode 104_二叉樹_遍歷】Maximum Depth of Binary TreeLeetCode二叉樹
- Leetcode226.翻轉二叉樹 Invert Binary Tree(Java)LeetCode二叉樹Java
- 自上而下列印二叉樹二叉樹
- LeetCode 156 Binary Tree Upside Down 上下翻轉二叉樹LeetCodeIDE二叉樹
- 【LeetCode 111_二叉樹_遍歷】Minimum Depth of Binary TreeLeetCode二叉樹
- Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍歷二叉樹的元素集合二叉樹
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉樹的最小共同父節點二叉樹
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉樹的各層建立連結串列二叉樹
- Construct Binary Tree from Preorder and Inorder Traversal(前序遍歷和中序遍歷樹構造二叉樹)...Struct二叉樹
- Amazing tree —— 二叉查詢樹
- LeetCode 297. Serialize and Deserialize Binary Tree 二叉樹序列化反序列化LeetCode二叉樹
- JS中的演算法與資料結構——二叉查詢樹(Binary Sort Tree)JS演算法資料結構
- 【LeetCode 235_二叉搜尋樹】Lowest Common Ancestor of a Binary Search TreeLeetCode
- 山東省第五屆ACM大學生程式設計競賽-Full Binary Tree(二叉樹&&求任意兩節點路徑)ACM程式設計二叉樹
- Python演算法和資料結構:在二叉樹中找到和為sum的所有路徑Python演算法資料結構二叉樹
- 32 從上到下列印二叉樹二叉樹
- 二叉樹路徑總和二叉樹
- 二叉樹路徑查詢二叉樹
- POJ2255Tree Recovery(二叉樹)二叉樹
- LeetCode 劍指offer——從上到下列印二叉樹 II、從上到下列印二叉樹 IIILeetCode二叉樹
- 二叉樹最大距離(直徑)二叉樹
- 22.從上往下列印二叉樹二叉樹
- 二叉樹實現按層 s型列印二叉樹