![](/img/trans.png)
[英]Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree
[英]Verify that all nodes at given level have different values in binary tree
我的問題是,這個問題可以在不使用任何數據結構(堆棧,列表等)的情況下解決,還是需要一個? (如果可能的話,我也希望看到兩種情況下的解決方案)。
問題:
有一個BinaryTree類,表示包含整數值的二叉樹。 假設已經實現了方法:
public BinaryTree right(); // returns right children
public BinaryTree left(); // returns left children
public int val(); // returns value of root node.
實現以下遞歸方法:
public static boolean allDifferentAtLevel(BinaryTree a, int lev){...}
接收整數的二叉樹模型和返回true只有在水平列弗所有的節點的所有值具有不同的值。
在此先感謝您的時間。
我們可以使用HashSet<Integer>
來跟蹤lev
級別的數據。
public static boolean allDifferentAtLevel(BinaryTree a, int lev){
return checker(new HashSet<Integer>(),a,0,lev); //EmptySet, Root, CurrentLevel, Level
}
public static boolean checker(HashSet<Integer> set,BinaryTree a, int currentLevel, int lev) {
if(a==null) //If current node of tree is null, return true.
return true;
if(currentLevel==lev) //If currentLevel is the required level, We don't need to move further.
//We can just validate the data at currentLevel and return the appropriate value.
{
int value=a.val();
if(set.contains(value)) //If set contains the value, Duplication found.
return false;
set.add(value);
return true;
}
//Check for both of the children.
return checker(set,a.left(),currentLevel+1,lev) && checker(set,a.right(),currentLevel+1,lev);
}
然而,它可能效率低下 - 您可以實現兩個遞歸函數:
是的,這可以通過遞歸來解決,而無需額外的數據結構。
讓我們嘗試定義allDifferentAtLevel(BinaryTree tree, int lev)
方法對於不同的lev
應該是什么樣子:
對於lev=0
,結果只是false
因為級別0
上的所有節點具有相同的值。 級別0
上只有一個節點,因此所有節點都具有相同的值。
對於lev=1
,檢查tree.right().val() == tree.left().val()
(添加null
檢查)是非常簡單的。
對於更高級別( lev>1
),您應該遞歸調用該方法。 基本上, allDifferentAtLevel(tree.left(), lev - 1)
和allDifferentAtLevel(tree.right(), lev - 1)
將確保左子樹和右子樹滿足條件。 不幸的是,這是不夠的,因為左右子樹之間可能有一些共同的價值。
但是可以解決這個檢查,不僅僅是左右,而是所有子樹的組合更深層次。 就像是:
BinaryTree ll = tree.left() == null ? null : tree.left().left();
BinaryTree lr = tree.left() == null ? null : tree.left().right();
BinaryTree rl = tree.right() == null ? null : tree.right().left();
BinaryTree rr = tree.right() == null ? null : tree.right().right();
BinaryTree ll_lr = tree.left();
BinaryTree ll_rl = new BinaryTree(0, ll, rl);
BinaryTree ll_rr = new BinaryTree(0, ll, rr);
BinaryTree lr_rl = new BinaryTree(0, lr, rl);
BinaryTree lr_rr = new BinaryTree(0, lr, rr);
BinaryTree rl_rr = tree.right();
return allDifferentAtLevel(ll_lr, lev - 1) &&
allDifferentAtLevel(ll_rl, lev - 1) &&
allDifferentAtLevel(ll_rr, lev - 1) &&
allDifferentAtLevel(lr_rl, lev - 1) &&
allDifferentAtLevel(lr_rr, lev - 1) &&
allDifferentAtLevel(rl_rr, lev - 1);
有兩個更深層次的子樹( ll
, lr
, rl
, rr
)。 但我們不能只檢查這些子樹,我們必須互相檢查它們。 這些子樹有六種可能的不同對。 為了相互檢查這些子樹,我們可以為每個不同的對( ll_lr
, ll_rl
, ll_rr
, lr_rl
, lr_rr
, rl_rr
)創建一個二叉樹,然后遞歸地檢查這些二進制樹中的每一個。 如果任何子樹ll
, lr
, rl
, rr
在lev-2
具有相等的元素,則它們將具有在lev-1
上具有相等元素的一對。
所以是的,這個問題可以通過遞歸來解決,而無需額外的數據結構。 我不認為BinaryTree
是一個額外的數據結構。
話雖如此,使用像集合這樣的附加數據結構可以使任務更容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.