中序遍历

This commit is contained in:
Nezuko-Wen 2020-12-05 17:35:53 +08:00
parent 3df2a17f7f
commit e93ee15749
1 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package leecode.tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* 二叉树的中序遍历
*/
public class No94 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
ret.add(cur.val);
cur = cur.right;
}
}
return ret;
}
}