力扣hot00 二叉树的中序遍历

‍ 题目地址

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
	public List<Integer> inorderTraversal(TreeNode root)
	{
		List<Integer> ans = new ArrayList<>();
        if(root == null)
            return ans;
		if (root.left != null)
			ans = inorderTraversal(root.left);
		ans.add(root.val);
		if (root.right != null)
			ans.addAll(inorderTraversal(root.right));
		return ans;
	}
}

你可能感兴趣的:(力扣,hot100,leetcode,算法,职场和发展)