[LintCode] Check Full Binary Tree

linspiration發表於2019-01-19

Description

A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has one child node. More information about full binary trees can be found here.

Full Binary Tree

      1
     / 
    2   3
   / 
  4   5

Not a Full Binary Tree

      1
     / 
    2   3
   / 
  4   

Have you met this question in a real interview?

Example

Given tree {1,2,3}, return true
Given tree {1,2,3,4}, return false
Given tree {1,2,3,4,5} return true

Solution

public class Solution {
    /**
     * @param root: the given tree
     * @return: Whether it is a full tree
     */
    public boolean isFullTree(TreeNode root) {
        // write your code here
        if (root == null) return true;
        if (root.left == null && root.right == null) return true;
        if (root.left == null && root.right != null) return false;
        if (root.left != null && root.right == null) return false;
        return isFullTree(root.left) && isFullTree(root.right);
    }
}

相關文章