日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[Leetcode][第257题][JAVA][二叉树的所有路径][BFS][DFS]

發(fā)布時(shí)間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Leetcode][第257题][JAVA][二叉树的所有路径][BFS][DFS] 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【問(wèn)題描述】[簡(jiǎn)單]

【解答思路】

1. DFS

時(shí)間復(fù)雜度:O(N^2) 空間復(fù)雜度:O(N^2)

class Solution {public List<String> binaryTreePaths(TreeNode root) {List<String> paths = new ArrayList<String>();constructPaths(root, "", paths);return paths;}public void constructPaths(TreeNode root, String path, List<String> paths) {if (root != null) {StringBuffer pathSB = new StringBuffer(path);pathSB.append(Integer.toString(root.val));if (root.left == null && root.right == null) { // 當(dāng)前節(jié)點(diǎn)是葉子節(jié)點(diǎn)paths.add(pathSB.toString()); // 把路徑加入到答案中} else {pathSB.append("->"); // 當(dāng)前節(jié)點(diǎn)不是葉子節(jié)點(diǎn),繼續(xù)遞歸遍歷constructPaths(root.left, pathSB.toString(), paths);constructPaths(root.right, pathSB.toString(), paths);}}} }

時(shí)間復(fù)雜度更高 ,使用String+“ ”拼接字符串

class Solution {//這行代碼可不敢寫(xiě)這里,leetcode提交會(huì)出問(wèn)題的。。。。!!!//private static List<String> res = new ArrayList<String>();public List<String> binaryTreePaths(TreeNode root) {List<String> res = new ArrayList<String>();if(root == null)return res;BL(root,res,"");return res;}public static void BL(TreeNode node,List<String> res,String str){if(node.left != null) BL(node.left, res, str + node.val + "->");if(node.right != null) BL(node.right, res, str + node.val + "->");if(node.left == null && node.right == null) res.add(str + node.val);} }鏈接:https://leetcode-cn.com/problems/binary-tree-paths/solution/chang-gui-cao-zuo-50di-gui-by-gu-xiong-007/
2. BFS

時(shí)間復(fù)雜度:O(N2) 空間復(fù)雜度:O(N2)

class Solution {public List<String> binaryTreePaths(TreeNode root) {List<String> paths = new ArrayList<String>();if (root == null) {return paths;}Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();Queue<String> pathQueue = new LinkedList<String>();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) {paths.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 paths;} }

【總結(jié)】

1. String StringBuffer StringBuilder
2.二叉樹(shù)遍歷
  • 前序遍歷 先輸出當(dāng)前結(jié)點(diǎn)的數(shù)據(jù),再依次遍歷輸出左結(jié)點(diǎn)和右結(jié)點(diǎn)
  • 中序遍歷 先遍歷輸出左結(jié)點(diǎn),再輸出當(dāng)前結(jié)點(diǎn)的數(shù)據(jù),再遍歷輸出右結(jié)點(diǎn)
  • 后續(xù)遍歷 先遍歷輸出左結(jié)點(diǎn),再遍歷輸出右結(jié)點(diǎn),最后輸出當(dāng)前結(jié)點(diǎn)的數(shù)據(jù)
3.復(fù)制粘貼時(shí)候要小心 修改相應(yīng)代碼 腦子模擬代碼 快速排查錯(cuò)誤 多從自己寫(xiě)的代碼 切忌浮躁

轉(zhuǎn)載鏈接:https://leetcode-cn.com/problems/binary-tree-paths/solution/er-cha-shu-de-suo-you-lu-jing-by-leetcode-solution/
參考鏈接:https://leetcode-cn.com/problems/binary-tree-paths/solution/chang-gui-cao-zuo-50di-gui-by-gu-xiong-007/

總結(jié)

以上是生活随笔為你收集整理的[Leetcode][第257题][JAVA][二叉树的所有路径][BFS][DFS]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。