给定一个二叉树的根节点root,返回它的中序遍历结果。
数据范围:树上节点数满足 ,树上每个节点的值满足
进阶:空间复杂度 ,时间复杂度
进阶:空间复杂度 ,时间复杂度
{1,2,#,#,3}
[2,3,1]
{}
[]
{1,2}
[2,1]
{1,#,2}
[1,2]
树中节点数目在范围 [0, 100] 内树中的节点的值在[-100,100]以内
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型vector */ vector<int> inorderTraversal(TreeNode* root) { // write code here vector<int> inOrderSeq; if(root == nullptr) return inOrderSeq; TreeNode* cur = root; TreeNode* mostRight = nullptr; while(cur != nullptr){ mostRight = cur->left; if(mostRight != nullptr){ while(mostRight->right != nullptr && mostRight->right != cur) mostRight = mostRight->right; if(mostRight->right == nullptr){ mostRight->right = cur; cur = cur->left; continue; }else{ // 会到达两次的节点在第二次到达时追加 inOrderSeq.push_back(cur->val); mostRight->right = nullptr; } }else{ // 只会到达一次的节点直接追加 inOrderSeq.push_back(cur->val); } cur = cur->right; } return inOrderSeq; } };
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型一维数组 */ public int[] inorderTraversal (TreeNode root) { List<Integer> list = new ArrayList<>(); Stack<TreeNode> s = new Stack<>(); if (root == null){ return new int[0]; } TreeNode current = root; while (!s.empty()||current!=null){ while(current!=null){ s.push(current); current = current.left; } if (!s.empty()){ TreeNode tree = s.pop(); list.add(tree.val); current = tree.right; } } int[] res = new int[list.size()]; for (int i = 0; i < list.size(); i++) { res[i] = list.get(i); } return res; } }
static int count = 0; void inOrder(struct TreeNode* root,int *a){ if(root != NULL){ inOrder(root->left, a); a[count++] = root->val; inOrder(root->right, a); } } int* inorderTraversal(struct TreeNode* root, int* returnSize ) { int *a = (int*)malloc(sizeof(int)*1001); inOrder(root, a); *returnSize = count; return a; // write code here }
ArrayList<Integer> inList = new ArrayList<>(); public int[] inorderTraversal (TreeNode root) { // write code here inOrder(root); return inList.stream().mapToInt(Integer::valueOf).toArray(); } public void inOrder(TreeNode root){ if(root==null){ return; } inOrder(root.left); inList.add(root.val); inOrder(root.right); }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型vector */ vector<int> res; vector<int> inorderTraversal(TreeNode* root) { if(root==nullptr)return res; inorderTraversal(root->left); res.push_back(root->val); inorderTraversal(root->right); return res; } };
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型vector */ vector<int> inorderTraversal(TreeNode* root) { // write code here vector<int> res; inorderTraversal(res,root); return res; } void inorderTraversal(vector<int> &res,TreeNode* root){ if(NULL==root) return; inorderTraversal(res,root->left); res.push_back(root->val); inorderTraversal(res,root->right); } };
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } pub fn inorderTraversal(&self, root: Option<Box<TreeNode>>) -> Vec<i32> { // write code here let mut res = vec![]; fn dfs(node: &Option<Box<TreeNode>>, arr: &mut Vec<i32>) { if let Some(ref node) = *node { dfs(&node.left, arr); arr.push(node.val); dfs(&node.right, arr); } } dfs(&root, &mut res); res } }
public class Solution { /** * 不断遍历左节点,同时压入栈,当没有左节点的时候证明到底了,可以将栈顶元素弹出放入集合 * 然后判断弹出元素的右节点,方法都是一样,不断循环这个过程,就可以完成中序遍历 * 注意结束条件是当前节点和队列都为空时 * * @param root TreeNode类 * @return int整型一维数组 */ public int[] inorderTraversal (TreeNode root) { LinkedList<TreeNode> stack = new LinkedList<>(); ArrayList<Integer> list = new ArrayList<>(); TreeNode curr = root; while(curr != null || !stack.isEmpty()){ if(curr != null){ stack.push(curr); curr = curr.left; }else{ TreeNode pop = stack.pop(); list.add(pop.val); curr = pop.right; } } int[] arr = new int[list.size()]; for(int i = 0; i < list.size(); i++){ arr[i] = list.get(i); } return arr; } }
let arr=[] function inorderTraversal( root ) { // write code here if(root==null) return []; if(root){ inorderTraversal(root.left); arr.push(root.val); inorderTraversal(root.right); } return arr; }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型一维数组 */ public ArrayList<Integer> list = new ArrayList<Integer>(); public void get(TreeNode root) { if (root == null) { return ; } get(root.left); list.add(root.val); get(root.right); } public int[] inorderTraversal (TreeNode root) { // write code here get(root); return list.stream().mapToInt(Integer::intValue).toArray(); } }
func inorderTraversal(root *TreeNode) []int { res := make([]int, 0) cur, mostRight := root, &TreeNode{} for cur != nil { mostRight = cur.Left if mostRight != nil { for mostRight.Right != nil && mostRight.Right != cur { mostRight = mostRight.Right } if mostRight.Right != cur { mostRight.Right = cur cur = cur.Left continue } else { mostRight.Right = nil } } res = append(res, cur.Val) cur = cur.Right } return res }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型一维数组 */ public int[] inorderTraversal (TreeNode root) { if(root == null){ return new int[0]; } // 中序遍历: left -> this -> right // 存储中序遍历的结果 List<Integer> arrayList = new ArrayList(); // 中序遍历 inDFS(root, arrayList); // 转int数组 return arrayList.stream().mapToInt(i->i).toArray(); } public void inDFS (TreeNode root, List<Integer> arrayList) { // 节点空,返回 if(root == null){ return; } // 向左遍历 inDFS (root.left, arrayList); // 存储当前节点 arrayList.add(root.val); // 向右遍历 inDFS (root.right, arrayList); } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型vector */ vector<int> inorderTraversal(TreeNode* root) { // 时间复杂度O(N),空间复杂度O(N) vector<int> res; stack<TreeNode*> stk; TreeNode* node = root; while (!stk.empty() || node) { while (node) { stk.push(node); node = node->left; } node = stk.top(); stk.pop(); res.push_back(node->val); node = node->right; } return res; } };
迭代版
class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> ans; stack<TreeNode *> s; TreeNode *curr = root; while (curr) { while (curr) { s.push(curr); curr = curr->left; } while (!s.empty()) { curr = s.top(); s.pop(); ans.push_back(curr->val); if (curr->right) break; } if (curr->right) curr = curr->right; else break; } return ans; } };