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

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

生活随笔

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

编程问答

lintcode:二叉树的中序遍历

發(fā)布時(shí)間:2025/3/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lintcode:二叉树的中序遍历 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:

二叉樹(shù)的中序遍歷

給出一棵二叉樹(shù),返回其中序遍歷

樣例

給出二叉樹(shù)?{1,#,2,3},

1\2/3

返回?[1,3,2].

挑戰(zhàn)

你能使用非遞歸算法來(lái)實(shí)現(xiàn)么?

解題:

程序直接來(lái)源

Java程序:

/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left = this.right = null;* }* }*/ public class Solution {/*** @param root: The root of binary tree.* @return: Inorder in ArrayList which contains node values.*/public ArrayList<Integer> inorderTraversal(TreeNode root) {// write your code hereArrayList<TreeNode> p = new ArrayList<TreeNode>(); ArrayList<Integer> res = new ArrayList<Integer>(); while(root != null || p.size() != 0){ while(root != null){ p.add(root); root = root.left; } root = p.get(p.size()-1); p.remove(p.size()-1); res.add(root.val); root = root.right; } return res; }} View Code

總耗時(shí):?1238?ms

Python程序:

""" Definition of TreeNode: class TreeNode:def __init__(self, val):self.val = valself.left, self.right = None, None """class Solution:"""@param root: The root of binary tree.@return: Inorder in ArrayList which contains node values."""def inorderTraversal(self, root):# write your code herep = [root] res = [0] while root is not None or len(p) != 1: while root is not None: p.append(root) root = root.left root = p[len(p)-1] del p[len(p)-1] res.append(root.val) root = root.right n = len(res) return res[1:n] View Code

總耗時(shí):?263?ms

非遞歸程序,理解不透,還需要人丑就要多讀書(shū)

根據(jù)上面靈感,遞歸程序如下:

java程序:

/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left = this.right = null;* }* }*/ public class Solution {/*** @param root: The root of binary tree.* @return: Inorder in ArrayList which contains node values.*/public ArrayList<Integer> inorderTraversal(TreeNode root) {// write your code here ArrayList<Integer> res = new ArrayList<Integer>(); res = inorderTrun(res,root);return res; }public ArrayList<Integer> inorderTrun(ArrayList<Integer> res,TreeNode root){if(root == null)return res;if(root!=null){if(root.left!=null){res = inorderTrun(res,root.left);}res.add(root.val);if(root.right!=null){res = inorderTrun(res,root.right);}}return res;}} View Code

總耗時(shí):?1714?ms

Python程序:

""" Definition of TreeNode: class TreeNode:def __init__(self, val):self.val = valself.left, self.right = None, None """class Solution:"""@param root: The root of binary tree.@return: Inorder in ArrayList which contains node values."""def inorderTraversal(self, root):# write your code hereres = []res = self.inorderTrun(res,root)return resdef inorderTrun(self,res,root):if root==None:return resif root.left!=None:res = self.inorderTrun(res,root.left)res.append(root.val)if root.right!=None:res = self.inorderTrun(res,root.right)return res View Code

總耗時(shí):?213?ms

?根據(jù)上面的程序理解,可根據(jù)棧實(shí)現(xiàn),上面定義的ArrayList也是起到棧的作用

?

總結(jié)

以上是生活随笔為你收集整理的lintcode:二叉树的中序遍历的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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