Short JAVA DFS


  • 0
    W
    public class Solution {
        List<Integer> res;
        
        public List<Integer> inorderTraversal(TreeNode root) {
            res = new ArrayList<>();
            dfs(root);
            return res;
        }
        private void dfs(TreeNode node) {
            if (node == null) return;
            dfs(node.left);
            res.add(node.val);
            dfs(node.right);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.