目录
题目:
解题思路:
python解法1:
python解法2:
java解法:
scala解法:
感慨:
请写一个函数 void print(List
把输入的数据在控制台打印为一个有缩进的树形结构.
list_ = [
[1, 0, "AA"],
[2, 1, "BB"],
[3, 1, "CC"],
[4, 3, "DD"],
[5, 3, "EE"],
[6, 2, "FF"],
[7, 2, "GG"],
[8, 4, "HH"],
[9, 5, "II"]
]
list_ = [i for i in list_ if not i.append([])] # 初始化 index=3处为空列表,表示附属子节点id组成的列表;
# print(list_)
# 链表做反向,方便后续正向深度便利
for i in list_:
if i[1] == 0:
pass
else:
list_[i[1] - 1][3].append(i[0])
print(list_)
level = 0
def getnextandprint(id:int):
# 深度递归打印 level控制级别 即缩进
node = list_[id - 1]
global level
print(" " * level + node[2])
try:
# print("进入下级")
for a_nextNode_index in node[3]:
level += 1
getnextandprint(a_nextNode_index)
level -= 1
except:
pass
def printname(list_ = list_):
# 获取root,调用深度递归函数 getnextandprint
for index in range(0,len(list_)):
if list_[index][1] == 0:
getnextandprint(list_[index][0])
break
if __name__ == '__main__':
printname()
class Node:
def __init__(self, id, parentid,name):
self.id = id
self.parentid = parentid
self.name = name
self.nextnodelist = []
if __name__ == '__main__':
list_ = []
list_.append(Node(1,0,"AA"))
list_.append(Node(2,1,"BB"))
list_.append(Node(3,1,"CC"))
list_.append(Node(4,3,"DD"))
list_.append(Node(5,3,"EE"))
list_.append(Node(6,2,"FF"))
list_.append(Node(7,2,"GG"))
list_.append(Node(8,4,"HH"))
list_.append(Node(9,5,"II"))
for anode in list_:
if anode.parentid == 0:
# 初始node
pass
else:
list_[anode.parentid - 1].nextnodelist.append(anode)
# level记录层级关系
level = 0
# for print
def getnextandprint(node):
global level
print(" "*level + node.name)
try:
# print("进入下级")
for a_nextNode in node.nextnodelist:
level += 1
getnextandprint(a_nextNode)
level -= 1
except :
pass
# for print
def printname(list_):
getnextandprint(list_[0])
printname(list_)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class list_reverse_opp {
// level 控制缩进或者层级
static int level = 0;
public static void main(String[] args) {
List nodes = Arrays.asList(
new Node(1, 0, "AA"),
new Node(2, 1, "BB"),
new Node(3, 1, "CC"),
new Node(4, 3, "DD"),
new Node(5, 3, "EE"),
new Node(6, 2, "FF"),
new Node(7, 2, "GG"),
new Node(8, 4, "HH"),
new Node(9, 5, "II")
);
print_node(nodes);
}
static void run(int index,List nodes) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(nodes.get(index).name);
for (Node node : nodes.get(index).nextNodeList) {
try {
level += 1;
run(node.id - 1,nodes);
level -= 1;
} catch (Exception e) {
// e.printStackTrace(); // 获取异常类型 性质 栈层次 报错位于程序的位置
// e.getMessage(); //获取错误性质
// e.toString(); // 输出异常的类型和性质
}
}
}
static void print_node(List nodes) {
// 添加子节点信息
nodes.forEach(node -> {
if (node.parentid != 0) {
nodes.get(node.parentid - 1).nextNodeList.add(node);
}
});
run(0,nodes);
}
}
class Node {
int id;
int parentid;
String name;
List nextNodeList = new ArrayList<>();
public Node(int id, int parentid, String name) {
this.id = id;
this.parentid = parentid;
this.name = name;
}
}
import scala.collection.mutable.ArrayBuffer
object list_reverse_oop {
def main(args: Array[String]): Unit = {
val aa = 2;
val arrayBuffer: ArrayBuffer[(Int, Int, String)] = ArrayBuffer(
(1, 0, "AA"),
(2, 1, "BB"),
(3, 1, "CC"),
(4, 3, "DD"),
(5, 3, "EE"),
(6, 2, "FF"),
(7, 2, "GG"),
(8, 4, "HH"),
(9, 5, "II")
)
val nodes: ArrayBuffer[Node1] = arrayBuffer.map {
case (id, fid, name) => Node1(id = id, fatherID = fid, name = name, sonList = new ArrayBuffer[Node1]())
}
// 指针反转
nodes.foreach(node => {
if (node.fatherID != 0) {
nodes(node.fatherID - 1).sonList.append(node);
}
})
var level = 0;
printNode()
def printNode(): Unit = {
run(0)
}
def run(index: Int): Unit = {
for (i <- 0 until level) {
System.out.print(" ")
}
println(nodes(index).name)
import scala.collection.JavaConversions._
for (node <- nodes(index).sonList) {
try {
level += 1
run(node.id - 1)
level -= 1
} catch {
case e: Exception =>
}
}
}
}
}
case class Node1(id: Int, fatherID: Int, name: String, sonList: ArrayBuffer[Node1])
很好的公司,面试官也很好,算法题不多刷刷很容易脑子打结,回来后没查资料自己接出来了.