麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

257. Binary Tree Paths(打印二叉樹所有路徑)

2019-11-14 09:13:09
字體:
供稿:網(wǎng)友
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / /2 3 / 5All 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 = Noneclass 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

方法二:采用廣度優(yōu)先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
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久久久久久久免费 | 欧美黄色看| 久久精品亚洲一区 | 欧美精品成人一区二区在线观看 | 午夜神马福利视频 | 成人精品视频网站 | 久久精品视频国产 | 斗破苍穹在线观看免费完整观看 | 亚洲午夜一区二区三区 | 久久精品中文字幕一区二区三区 | 国产一区精品在线观看 | 欧美一级做一a做片性视频 日韩黄色片免费看 | 久久99国产精品免费网站 | 久久久久九九九女人毛片 | 亚洲最新黄色网址 | 久色亚洲 | 欧美 日韩 国产 在线 | 欧美 国产 综合 | 毛片免费大全短视频 | 欧美日韩网站在线观看 | 日日噜噜夜夜爽 | 91精品国产综合久久婷婷香蕉 | 久久欧美亚洲另类专区91大神 | 国产一级一片免费播放 | 特级西西444www大精品视频免费看 | 有兽焉免费动画 | 一级一级一级一级毛片 | 欧美a视频| 青青草成人免费视频在线 | 国产一级在线免费观看 | 久久综合一区 | 黄网站免费观看视频 | 97人人草 | 久久精品99北条麻妃 | 亚洲国产视频网 | 国产成人精品自拍视频 | 久草欧美| 国产精品久久久久久久亚洲按摩 | 欧洲成人综合网 | 91丝袜| 日本在线播放一区二区三区 |