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?
ExampleGiven tree {1,2,3}, return true
Given tree {1,2,3,4}, return false
Given tree {1,2,3,4,5} return true
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); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/69436.html
problem Check if two binary trees are identical. Identical means the two binary trees have the same structure and every identical position has the same value. Example 1 1 / ...
摘要:這里要注意的是的用法。所以記住,用可以從自動(dòng)分離出數(shù)組。跳過(guò)第一個(gè)元素并放入數(shù)組最快捷語(yǔ)句建立的用意記錄處理過(guò)的結(jié)點(diǎn)并按處理所有結(jié)點(diǎn)和自己的連接下面先通過(guò)判斷,再修改的符號(hào)的順序,十分巧妙更輕便的解法 Problem Design an algorithm and write code to serialize and deserialize a binary tree. Writin...
摘要:根據(jù)二叉平衡樹(shù)的定義,我們先寫(xiě)一個(gè)求二叉樹(shù)最大深度的函數(shù)。在主函數(shù)中,利用比較左右子樹(shù)的差值來(lái)判斷當(dāng)前結(jié)點(diǎn)的平衡性,如果不滿足則返回。 Problem Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as...
摘要:建立兩個(gè)樹(shù)結(jié)點(diǎn),先用找到在的位置,讓作為的根節(jié)點(diǎn)找到的位置后,指向。此時(shí),用代替與連接就可以了。 Problem Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tr...
Problem Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You sho...
閱讀 2010·2021-09-22 16:05
閱讀 9324·2021-09-22 15:03
閱讀 2889·2019-08-30 15:53
閱讀 1704·2019-08-29 11:15
閱讀 913·2019-08-26 13:52
閱讀 2356·2019-08-26 11:32
閱讀 1808·2019-08-26 10:38
閱讀 2571·2019-08-23 17:19