我很困惑如何在穿越BFS时保持树的顶部。我接受这样的输入:
5 // total number of nodes in tree (root node will always be 1)
// this number also tells the number of lines that'll be coming up
2 3 // left and right child of root, tree be like : 1
/ \
2 3
4 5 // left and right child of 2 , tree 1
/ \
2 3
/ \
4 5
-1 -1 // left and right child of 3 , that's null
-1 -1 // left and right child of 4
-1 -1 // left and right child of 5
这继续以上述方式,用户在BFS中输入左右孩子。但我无法理解如何实现这一目标。
我的看法是:
LinkedList<Node> list = new LinkedList<Node>
list.add(root); //initially push root
while(list.peek()){ //while there is node left in linked list
curr = list.poll();
curr.left = new Node(x) // x = whatever is the user input is (using nextInt)
// and if it's -1, i'll make left child null
curr.right = new Node(x) // same goes for this one
ll.add(curr);
}
最后,我需要对根节点的引用,我不知道如何获取它?而且,有没有更好的方法来实现这个任务
答案 0 :(得分:1)
我希望下面的代码可以帮到你。
“readTree”函数用于读取树。
<img ng-src="currentImg"> <!-- this is the default image -->
<!-- i need to swap current image with loading.gif and then display selected img from below -->
<!-- on click is changing currentImg (default image) var to ng-src -->
<img ng-click="currentImg=item.imgSmall1" ng-src="{{item.imgSmall1}}">
<img ng-click="currentImg=item.imgSmall2" ng-src="{{item.imgSmall2}}">
...