美文网首页
Leetcode - 104. Validate Binary

Leetcode - 104. Validate Binary

作者: whicter | 来源:发表于2016-08-25 02:06 被阅读0次

    题目描述

    Given a binary tree, find its maximum depth.

    The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

    原题链接

    Java代码实现1 - 递归

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int maxDepth(TreeNode root) {
            return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
        }
    }
    

    Java代码实现2 - BFS

    public class Solution {
        public int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            int count = 0;
            while (!queue.isEmpty()) {
                int remainingLevelSize = queue.size();
                while (remainingLevelSize-- > 0) {
                    TreeNode curNode = queue.poll();
                    nodeEnqueue(curNode.left, queue);
                    nodeEnqueue(curNode.right, queue);
                }
                count ++;
            } 
            return count;
        }
        
        private void nodeEnqueue(TreeNode curNode, Queue<TreeNode> queue) {
            if (curNode != null) {
                queue.offer(curNode);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Leetcode - 104. Validate Binary

          本文链接:https://www.haomeiwen.com/subject/dkwosttx.html