二叉樹的遍歷


先序遍歷(Preorder Traversal) 根-左-右 1. 遞歸 Java
 1 /**
2 * Definition for a binary tree node.
3 * public class TreeNode {
4 * int val;
5 * TreeNode left;
6 * TreeNode right;
7 * TreeNode(int x) { val = x; }
8 * }
9 */
10 class Solution {
11 public List<Integer> preorderTraversal(TreeNode root) {
12 List<Integer> result = new ArrayList<Integer>();
13 if (root != null) {
14 result.add(root.val);
15 result.addAll(preorderTraversal(root.left));
16 result.addAll(preorderTraversal(root.right));
17 }
18 return result;
19 }
20 }// 1 ms

Python

 1 # Definition for a binary tree node.
2 # class TreeNode(object):
3 # def __init__(self, x):
4 # self.val = x
5 # self.left = None
6 # self.right = None
7
8 class Solution(object):
9 def preorderTraversal(self, root):
10 """
11 :type root: TreeNode
12 :rtype: List[int]
13 """
14 if root == None:
15 return []
16 return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right) 
2. 棧-迭代(入棧時記錄值)  Java
 1 /**
2 * Definition for a binary tree node.
3 * public class TreeNode {
4 * int val;
5 * TreeNode left;
6 * TreeNode right;
7 * TreeNode(int x) { val = x; }
8 * }
9 */
10 class Solution {
11 public List<Integer> preorderTraversal(TreeNode root) {
12 List<Integer> result = new ArrayList<Integer>();
13 Stack<TreeNode> stack = new Stack<TreeNode>();
14 while(root != null || !stack.empty()) {
15 if(root != null) {
16 result.add(root.val);
17 stack.add(root);
18 root = root.left;
19 } else {
20 root = stack.pop().right;
21 }
22 }
23 return result;
24 }
25 }// 2 ms

Python

 1 # Definition for a binary tree node.
2 # class TreeNode(object):
3 # def __init__(self, x):
4 # self.val = x
5 # self.left = None
6 # self.right = None
7
8 class Solution(object):
9 def preorderTraversal(self, root):
10 """
11 :type root: TreeNode
12 :rtype: List[int]
13 """
14 stack, result =[], []
15 while stack or root:
16 if root:
17 stack.append(root)
18 result.append(root.val)
19 root = root.left
20 else:
21 root = stack.pop().right
22 return result

 

中序遍歷(Inorder Traversal) 左-根-右 1. 遞歸 Java
 1 /**
2 * Definition for a binary tree node.
3 * public class TreeNode {
4 * int val;
5 * TreeNode left;
6 * TreeNode right;
7 * TreeNode(int x) { val = x; }
8 * }
9 */
10 class Solution {
11 public List<Integer> inorderTraversal(TreeNode root) {
12 List<Integer> result = new ArrayList<Integer>();
13 if(root != null) {
14 result.addAll(inorderTraversal(root.left));
15 result.add(root.val);
16 result.addAll(inorderTraversal(root.right));
17 }
18 return result;
19 }
20 }// 1 ms
Python
 1 # Definition for a binary tree node.
2 # class TreeNode(object):
3 # def __init__(self, x):
4 # self.val = x
5 # self.left = None
6 # self.right = None
7
8 class Solution(object):
9 def inorderTraversal(self, root):
10 """
11 :type root: TreeNode
12 :rtype: List[int]
13 """
14 if root == None:
15 return []
16 return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
2. 棧-迭代(出棧時記錄值) Java
 1 /**
2 * Definition for a binary tree node.
3 * public class TreeNode {
4 * int val;
5 * TreeNode left;
6 * TreeNode right;
7 * TreeNode(int x) { val = x; }
8 * }
9 */
10 class Solution {
11 public List<Integer> inorderTraversal(TreeNode root) {
12 List<Integer> result = new ArrayList<Integer>();
13 Stack<TreeNode> stack = new Stack<TreeNode>();
14 TreeNode tmpNode = null;
15 while(root != null || !stack.empty()) {
16 if(root != null) {
17 stack.push(root);
18 root = root.left;
19 } else {
20 tmpNode = stack.pop();
21 result.add(tmpNode.val);
22 root = tmpNode.right;
23 }
24 }
25 return result;
26 }
27 }// 2 ms
Python
 1 # Definition for a binary tree node.
2 # class TreeNode(object):
3 # def __init__(self, x):
4 # self.val = x
5 # self.left = None
6 # self.right = None
7
8 class Solution(object):
9 def inorderTraversal(self, root):
10 """
11 :type root: TreeNode
12 :rtype: List[int]
13 """
14 stack, result = [], []
15 while stack or root:
16 if root:
17 stack.append(root)
18 root = root.left
19 else:
20 root = stack.pop()
21 result.append(root.val)
22 root = root.right
23 return result

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



二叉樹的遍歷 二叉樹的遍歷 二叉樹的遍歷 二叉樹的遍歷 二叉樹的遍歷 二叉樹的遍歷 二叉樹的遍歷 二叉樹的遍歷 二叉樹的遍歷 二叉樹的遍歷
 
粤ICP备14056181号  © 2014-2021 ITdaan.com